// pages/home/home.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [
      {
        id: 'form',
        name: '表单',
        open: false,
        pages: ['button', 'list', 'input', 'slider', 'uploader']
      },
      {
        id: 'widget',
        name: '基础组件',
        open: false,
        pages: ['article', 'badge', 'flex', 'footer', 'gallery', 'grid', 'icons', 'loadmore', 'panel', 'preview', 'progress']
      },
      {
        id: 'feedback',
        name: '操作反馈',
        open: false,
        pages: ['actionsheet', 'dialog', 'msg', 'picker', 'toast']
      },
      {
        id: 'nav',
        name: '导航相关',
        open: false,
        pages: ['navbar', 'tabbar']
      },
      {
        id: 'search',
        name: '搜索相关',
        open: false,
        pages: ['searchbar']
      }
    ],
    actionSheetHidden: true,
    actionSheetItems: [
      { bindtap: 'bindMenu1', txt: '分享给朋友' },
      { bindtap: 'bindMenu2', txt: '保存品牌名片' }
    ],
    menu: '',
    config: {
      tipsshow1: "none",
      tipsshow2: "none"
    },
    news:[]
  },
  kindToggle: function (e) {
    var id = e.currentTarget.id, list = this.data.list;
    for (var i = 0, len = list.length; i < len; ++i) {
      if (list[i].id == id) {
        list[i].open = !list[i].open
      } else {
        list[i].open = false
      }
    }
    this.setData({
      list: list
    });
  },
  actionSheetTap: function () {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  actionSheetbindchange: function () {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  bindMenu1: function () {
    this.setData({
      menu: 1,
      actionSheetHidden: !this.data.actionSheetHidden
    })
    this.onShareAppMessage()
  },
  bindMenu2: function () {
    this.setData({
      menu: 2,
      actionSheetHidden: !this.data.actionSheetHidden
    })
    var IMG_URL = 'https://caryls.ch-ina315.com/xcx/mingpian.png';
    wx.downloadFile({
      url: IMG_URL,
      success: function (res) {
        console.log(res)
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (res) {
            console.log(res)
          },
          fail: function (res) {
            console.log(res)
            console.log('fail')
          }
        })
      },
      fail: function () {
        console.log('fail')
      }
    })
  },
  /**
 * 长按复制
 */
  copy: function (e) {
    var that = this;
    console.log(e);
    wx.setClipboardData({
      data: 'xincheng',
      success: function (res) {
        wx.showToast({
          title: '复制成功',
        });
      }
    });
  },
  /**
 * 一键复制xincheng
 */
  copyBtn: function (e) {
    var that = this;
    wx.setClipboardData({
      data: 'CaryLsVip',
      success: function (res) {
        wx.showToast({
          title: '已复制',
        });
      }
    });
  },
  // 点击事件
  phonecallevent: function (e) {
    wx.makePhoneCall({
      phoneNumber: '4008910800'
    })
  },
  saveImgToPhotosAlbumTap: function () {
    　　　　　// 图片必须是 https 的
    var IMG_URL = 'https://caryls.ch-ina315.com/xcx/mingpian.png';
    wx.downloadFile({
      url: IMG_URL,
      success: function (res) {
        console.log(res)
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (res) {
            console.log(res)
          },
          fail: function (res) {
            console.log(res)
            console.log('fail')
          }
        })
      },
      fail: function () {
        console.log('fail')
      }
    })
  },
  //监控滚动条
  scroll: function (e) {
    this.setData({
      config: {
        tipsshow1: "",
        tipsshow2: "none"
      }
    });

  },
  onPageScroll: function (e) {
    //console.log(e);//{scrollTop:99}
    if (e.scrollTop<=120)
    {
      this.setData({
        config: {
          tipsshow1: "",
          tipsshow2: "none"
        }
      });
    }
    else
    {
      this.setData({
        config: {
          tipsshow1: "",
          tipsshow2: "flex"
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    /*wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#ff0000',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })*/
    var that = this
    wx.request({
      url: 'https://caryls.ch-ina315.com/public/wexin.php',
      data: { id: '123456vip' },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      //header: { apikey: 'a37c01591e47494fe320137dbc0fd423' }, // 设置请求的 header
      success: function (res) {
        // success
        //console.log(res)
        that.setData({ //这个位置应该用page的引用调用 setData是重新赋值
          //范围是正确的
          news: res.data
        });
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    const ctx = wx.createCanvasContext('myCanvas')

    ctx.setStrokeStyle('red')
    ctx.moveTo(5, 75)
    ctx.lineTo(295, 75)
    ctx.stroke()

    ctx.setFontSize(20)

    ctx.setTextBaseline('top')
    ctx.fillText('top', 5, 75)

    ctx.setTextBaseline('middle')
    ctx.fillText('middle', 50, 75)

    ctx.setTextBaseline('bottom')
    ctx.fillText('bottom', 120, 75)

    ctx.setTextBaseline('normal')
    ctx.fillText('normal', 200, 75)

    ctx.draw()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#ff0000',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
   /* this.setData({
      config: {
        tipsshow1: "",
        tipsshow2: "flex"
      }
    });*/
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
  
})